import path from "path";
import { Sequelize } from "sequelize-typescript";
import config from "../config";
// import { dbLogger } from './../logger/index'

const sequelize = new Sequelize(config.db.db_name as string, config.db.db_user as string, config.db.db_password as string, {
  dialect: 'mysql', // 数据库类型
  host: config.db.db_host as string,
  port: config.db.db_port as unknown as number,
  pool: { // 连接池设置
    max: 5, // 最大连接数
    idle: 30000,
    acquire: 60000
  },
//   logging: msg => dbLogger.info(msg), // 日志，不打印则可设置为 false
  models: [path.join(__dirname, '..', 'model/**/*.ts'), path.join(__dirname, '..', 'model/**/*.js')],
  // dialectOptions: {
  //   charset: 'utf8mb4', // 字符集
  //   collate: 'utf8mb4_bin' // 排序规则
  // },
  define: {
    freezeTableName: true, // sequelize会给表名自动添加为复数，
    timestamps: true, // 开启时间戳 create_at delete_at update_at
    paranoid: true, // 开启假删除
    createdAt: 'created_at',
    updatedAt: 'updated_at',
    deletedAt: 'deleted_at',
    charset: 'utf8mb4', // 字符集
    collate: 'utf8mb4_bin' // 排序规则
  },
  timezone: '+08:00', // 改为标准时区
})

export const db = async () => {
  try {
    await sequelize.authenticate();
    console.log('Connection has been established successfully.');
  } catch (error) {
    console.error('Unable to connect to the database:', error);
  }
}

export default sequelize
